package pl.minim.unidefCMS.objects {
	import com.googlecode.milib.util.MLArrayUtil;

	/**
	 * @author Marek Brun
	 */
	public class UnidefList extends AbstractUnidefObject {

		private var list:Array = [];
		public var clazz:String;

		public function UnidefList(clazz:String) {
			this.clazz = clazz;
		}

		public function addObject(unidef:AbstractUnidefObject):void {
			list.push(unidef);
			unidef.parent = this;
		}

		public function getList():Array {
			return list.concat();
		}

		public function removeObj(unidef:AbstractUnidefObject):void {
			MLArrayUtil.remove(list, unidef);
		}

		override public function getTypeName():String {
			return 'List';
		}

		override public function getDataAsTextPreview():String {
			if(list.length==1) {
				return '1 item';
			}
			return list.length+' items';
		}

		override public function toXML():XML {
			var xml:XML = <i/>;
			var i:uint;
			var loop:AbstractUnidefObject;
			for(i = 0;i < list.length;i++) {
				loop = list[i];
				xml.appendChild(loop.toXML());
			}
			return xml;
		}

		override public function reactToPredefinedObjectDelete(deletedObject:UnidefClass):void {
			var i:uint;
			var loop:AbstractUnidefObject;
			for(i = 0;i < list.length;i++) {
				loop = list[i];
				loop.reactToPredefinedObjectDelete(deletedObject);
			}
		}

		override public function exportToRawData():* {
			var arr:Array = [];
			var i:uint;
			var loop:AbstractUnidefObject;
			var raw:Object;
			for(i = 0;i < list.length;i++) {
				loop = list[i];
				raw = loop.exportToRawData();
				raw.listID = loop.id;
				arr.push(raw);
			}
			return arr;
		}
	}
}
